package com.xnyzc.lhy.didi.controller.platform;

import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.MD5Util;
import com.xnyzc.lhy.didi.tongcheng.entity.TongChengRv;
import com.xnyzc.lhy.didi.tongcheng.entity.param.order.*;
import com.xnyzc.lhy.didi.tongcheng.penum.ETongChengErrorCode;
import com.xnyzc.lhy.didi.tongcheng.service.order.TcOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @Auther: lihao
 * @Date: 2020/7/5 17:59
 * @Description: 同城订单控制器
 */
@Api(value = "同城订单API", tags = {"同城订单API"})
@RestController
@RequestMapping("oa/tc/order")
@Slf4j
public class TongChengController {
    //同程密钥
    @Value("${global.tc.token}")
    private String token;

    @Autowired
    private TcOrderService tcOrderService;

    @ApiOperation(value = "创建同城订单", notes = "创建同城订单")
    @PostMapping(value = "/create")
    public TongChengRv getTcCreateOrderInfo(@ApiParam(name = "createOrderParam", value = "创建同城订单")
                                            @RequestBody
                                                    TcCreateOrderParam createOrderParam) throws IOException {
        if (checkSign(createOrderParam, createOrderParam.getSign())) {
            return tcOrderService.getTcCreateOrderInfo(createOrderParam);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "获取配置的订单价格", notes = "获取配置的订单价格")
    @PostMapping(value = "/price")
    public TongChengRv getTcOrderPrice(@ApiParam(name = "createOrderParam", value = "获取配置的订单价格")
                                       @RequestBody
                                               TcOrderPriceParam orderPriceParam) {
        if (checkSign(orderPriceParam, orderPriceParam.getSign())) {
            return tcOrderService.getTcOrderPrice(orderPriceParam);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
       //return tcOrderService.getTcOrderPrice(orderPriceParam);
    }

    @ApiOperation(value = "取消订单", notes = "同程取消订单接口")
    @PostMapping(value = "/cencel")
    public TongChengRv cancelOrder(@ApiParam(name = "cencel", value = "同程取消订单接口") @RequestBody
                                           TcCancelOrderParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.cancelOrder(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "获取司机位置", notes = "同程获取司机最新位置")
    @PostMapping(value = "/driverLocation")
    public TongChengRv getDriverLocation(@ApiParam(name = "driverLocation", value = "同程获取司机最新位置") @RequestBody
                                                 TcDriverLocationParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.getDriverLocation(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "订单详情", notes = "同程获取订单详情")
    @PostMapping(value = "/orderInfo")
    public TongChengRv getOrderInfo(@ApiParam(name = "orderInfo", value = "同程获取订单详情") @RequestBody
                                            TcOrderInfoParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.getOrderInfo(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "支付状态同步", notes = "同程支付状态同步")
    @PostMapping(value = "/payStatus")
    public TongChengRv setPayStatus(@ApiParam(name = "支付状态同步", value = "同程支付状态同步") @RequestBody
                                            TcOrderPayStatusParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.orderPaySync(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "账单详情", notes = "同程获取账单详情")
    @PostMapping(value = "/accountInfo")
    public TongChengRv getAccountInfo(@ApiParam(name = "账单详情", value = "同程获取账单详情") @RequestBody
                                              TcOrderPayStatusParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.orderPaySync(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "申请发票", notes = "同程申请发票")
    @PostMapping(value = "/getInvoice")
    public TongChengRv getInvoice(@ApiParam(name = "同程申请发票", value = "同程申请发票情") @RequestBody
                                          TcInvoiceParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.getInvoice(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "发票状态查询 ", notes = "同程发票状态查询")
    @PostMapping(value = "/getInvoiceStatus")
    public TongChengRv getInvoiceStatus(@ApiParam(name = "同程发票状态查询", value = "同程发票状态查询") @RequestBody
                                                TcInvoiceQueryParam param) {
        if (checkSign(param, param.getSign())) {
            return tcOrderService.getInvoiceStatus(param);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    @ApiOperation(value = "获取附近司机信息", notes = "获取附近司机信息")
    @PostMapping(value = "/driver")
    public TongChengRv getTcDrivers(@ApiParam(name = "getTcDrivers", value = "获取附近司机信息") @RequestBody
                                            TcDriverParam driverParam) {
        if (checkSign(driverParam, driverParam.getSign())) {
            return tcOrderService.getTcDrivers(driverParam);
        } else {
            return TongChengRv.error(ETongChengErrorCode.sign_err);
        }
    }

    private boolean checkSign(Object object, String sign) {
        // 获取对象属性
        Map<String, Object> params = new HashMap<>();
        Class clazz = object.getClass();
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                field.setAccessible(true); // 私有属性必须设置访问权限
                Object value = field.get(object);
                params.put(name, value);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }
        //排除无效参数
        params.remove("sign");
        params.remove("serialVersionUID");
        //对参数名进行自然排序
        Set<Map.Entry<String, Object>> set = params.entrySet();
        List<Map.Entry<String, Object>> infoIds = new ArrayList<>(set);
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {

            @Override
            public int compare(Map.Entry<String, Object> obj1, Map.Entry<String, Object> obj2) {
                return obj1.getKey().compareTo(obj2.getKey());
            }
        });
        StringBuilder buffer = new StringBuilder();
        for (Map.Entry<String, Object> item : infoIds) {
            if (CheckUtil.strIsNotEmpty(item.getKey())) {
                String key = item.getKey();
                Object val = item.getValue();
                if (val != null) {
                    String string = val.toString();
                    if (string.length() > 0) {
                        buffer.append(key + "=" + val.toString());
                        buffer.append("&");
                    }
                }
            }
        }
        String string = buffer.toString();
        String mstri1 = string + "token=" + token;
        log.info("Token-" + mstri1);
        String msign = MD5Util.MD5(mstri1).toLowerCase();
        log.info("MySign-" + msign);
        log.info("TcSign-" + sign);
        return sign.equalsIgnoreCase(msign);
    }
}
